package com.red.star.wechat.data.mappers;

import com.red.star.wechat.data.core.base.MyMapper;
import com.red.star.wechat.data.entity.WhiteList;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @author nofish.yan@gmail.com
 * @date 2018/7/12.
 * 白名单数据访问接口
 */
public interface WhiteListMapper extends MyMapper<WhiteList> {

    WhiteList findWhiteListByName(@Param("name") String name);

    /**
     * 根据手机号查询有效白名单
     *
     * @param mobile 手机号
     * @return 白名单用户信息
     */
    @Select("SELECT * FROM t_white_list WHERE mobile = #{mobile} AND status = 0 limit 1")
    WhiteList findByMobile(@Param("mobile") String mobile);

    @Select("SELECT * FROM t_white_list WHERE mobile = #{mobile} AND status = 0 AND type = #{type}")
    WhiteList findByMobileAndType(@Param("mobile") String mobile, @Param("type") Integer type);

    @Select("SELECT * FROM t_white_list WHERE type = #{type} AND status = 0")
    List<WhiteList> findWhiteListByType(@Param("type") Integer type);

    @Select("SELECT COUNT(*) FROM t_white_list AS twl " +
            "JOIN t_vip AS tv ON twl.mobile = tv.mobile " +
            "JOIN t_fans AS tf ON tv.vip_open_id = tf.vip_open_id " +
            "WHERE tf.id = #{fansId} AND twl.type = #{type}")
    int countWhiteListWithType(@Param("fansId") Integer fansId, @Param("type") Integer type);

    List<WhiteList> queryWhiteList(WhiteList whiteList);

    @Select("SELECT * FROM t_white_list WHERE id=#{id}")
    WhiteList selectById(@Param("id") Integer id);

    void updateDeleteWhiteListById(@Param("id") Integer id);

    @Select("SELECT * FROM t_white_list WHERE status!=1")
    List<WhiteList> selectNotDelete();

    void insertWhiteList(@Param("whiteList")List<WhiteList> whiteList);

    List<WhiteList> selectByMobile(@Param("mobile") String mobile,@Param("id") Integer id);

    List<WhiteList> selectByType(@Param("type") Integer type);

    List<WhiteList> selectAllPlant();

    WhiteList selectByCustomerMobile(@Param("mobile") String mobile);

}
